package org.zz.tool;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class BMP {
    public BMP() {
    }

    public static int JUnsigned(int x) {
        return x >= 0?x:x + 256;
    }

    public static Bitmap Iso2Bimap(byte[] imgIsoBuf) {
        if(imgIsoBuf[0] == 70 && imgIsoBuf[1] == 73 && imgIsoBuf[2] == 82) {
            int iImgX = JUnsigned(imgIsoBuf[41] << 8) + JUnsigned(imgIsoBuf[42]);
            int iImgY = JUnsigned(imgIsoBuf[43] << 8) + JUnsigned(imgIsoBuf[44]);
            if(iImgX >= 0 && iImgX <= 1000) {
                if(iImgY >= 0 && iImgY <= 1000) {
                    byte[] imgBuf = new byte[iImgX * iImgY];
                    System.arraycopy(imgIsoBuf, 46, imgBuf, 0, iImgX * iImgY);
                    return Raw2Bimap(imgBuf, iImgX, iImgY);
                } else {
                    return null;
                }
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    public static Bitmap Raw2Bimap(byte[] imgBuf, int iImgX, int iImgY) {
        byte[] bmpBuf = new byte[iImgX * iImgY + 1078];
        Raw2Bmp(bmpBuf, imgBuf, iImgX, iImgY);
        return BitmapFactory.decodeByteArray(bmpBuf, 0, bmpBuf.length);
    }

    public static int Raw2Bmp(byte[] pBmp, byte[] pRaw, int X, int Y) {
        byte[] head = new byte[1078];
        byte[] temp = new byte[]{66, 77, 0, 0, 0, 0, 0, 0, 0, 0, 54, 4, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        System.arraycopy(temp, 0, head, 0, temp.length);
        head[18] = (byte)(X & 255);
        int num = X >> 8;
        head[19] = (byte)(num & 255);
        num >>= 8;
        head[20] = (byte)(num & 255);
        num >>= 8;
        head[21] = (byte)(num & 255);
        head[22] = (byte)(Y & 255);
        num = Y >> 8;
        head[23] = (byte)(num & 255);
        num >>= 8;
        head[24] = (byte)(num & 255);
        num >>= 8;
        head[25] = (byte)(num & 255);
        int j = 0;

        int i;
        for(i = 54; i < 1078; i += 4) {
            head[i] = head[i + 1] = head[i + 2] = (byte)j;
            head[i + 3] = 0;
            ++j;
        }

        System.arraycopy(head, 0, pBmp, 0, 1078);

        for(i = 0; i < Y; ++i) {
            System.arraycopy(pRaw, i * X, pBmp, 1078 + (Y - 1 - i) * X, X);
        }

        return 0;
    }

    public static int SaveData(String filepath, byte[] buffer, int size) {
        File f = new File(filepath);
        FileOutputStream fos = null;

        try {
            fos = new FileOutputStream(f);
        } catch (FileNotFoundException var8) {
            var8.printStackTrace();
            return -1;
        }

        try {
            fos.write(buffer, 0, size);
        } catch (IOException var7) {
            var7.printStackTrace();
        }

        try {
            fos.close();
            return 0;
        } catch (IOException var6) {
            var6.printStackTrace();
            return -2;
        }
    }
    public static Bitmap rawToBitmap(byte[] ucImgBuf, int iWidth, int iHeight){
        byte[] bmpimagebuff = new byte[iWidth * iHeight + 1078];
        Raw2Bmp(bmpimagebuff, ucImgBuf, iWidth, iHeight);
        return BitmapFactory.decodeByteArray(bmpimagebuff,0,bmpimagebuff.length);
    }

    public static int SaveBMP(String strFileName, byte[] ucImgBuf, int iWidth, int iHeight) {
        byte[] bmpimagebuff = new byte[iWidth * iHeight + 1078];
        Raw2Bmp(bmpimagebuff, ucImgBuf, iWidth, iHeight);
        return SaveData(strFileName, bmpimagebuff, bmpimagebuff.length);
    }

    public int SaveIsoImg(String strFileName, byte[] bIsoFingerImage) {
        if(bIsoFingerImage[0] == 70 && bIsoFingerImage[1] == 73 && bIsoFingerImage[2] == 82) {
            int iWidth = JUnsigned(bIsoFingerImage[41] << 8) + JUnsigned(bIsoFingerImage[42]);
            int iHeight = JUnsigned(bIsoFingerImage[43] << 8) + JUnsigned(bIsoFingerImage[44]);
            if(iWidth >= 0 && iWidth <= 1000) {
                if(iHeight >= 0 && iHeight <= 1000) {
                    byte[] imageBuf = new byte[iWidth * iHeight];
                    System.arraycopy(bIsoFingerImage, 46, imageBuf, 0, iWidth * iHeight);
                    return SaveBMP(strFileName, imageBuf, iWidth, iHeight);
                } else {
                    return -3;
                }
            } else {
                return -2;
            }
        } else {
            return -1;
        }
    }
}
